package JFrame.Admin.UserManager;

import entity.User;
import serive.AdminService;
import serive.AdminServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * @className: AdminQueryUserJFrame
 * @description: TODO 类描述
 * @author: Dong
 * @date: 2021/12/30
 **/
public class QueryUserJFrame extends JFrame {
    JLabel l_id;
    JTextField t_id;
    JButton b_query;
    JTable table_book;
    AdminService adminService = new AdminServiceImpl();

    public QueryUserJFrame() {
        this.setTitle("查询用户窗体");
        this.setSize(550, 650);
        init();
        this.setVisible(true);
    }

    public void init() {

        this.setLayout(null);
        JLabel l_tip = new JLabel("以下是所有用户:");
        l_id = new JLabel("用户编号");
        t_id = new JTextField();
        b_query = new JButton("查询");
        b_query.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new queryUserByUid(t_id.getText());
            }
        });
        Object[] title = {"用户ID", "密码", "身份证", "性别", "年龄", "地址", "姓名", "电话"};
        List<User> usersList = adminService.queryUser();
        Object[][] Users = new String[usersList.size() + 1][8];
        for (int i = 0; i < usersList.size(); i++) {
            for (int j = 0; j < 8; j++) {
                switch (j) {
                    case 0:
                        Users[i][j] = usersList.get(i).getuId();
                        break;
                    case 1:
                        Users[i][j] = usersList.get(i).getPassword();
                        break;
                    case 2:
                        Users[i][j] = usersList.get(i).getIdCard();
                        break;
                    case 3:
                        Users[i][j] = usersList.get(i).getSex();
                        break;
                    case 4:
                        Users[i][j] = String.valueOf(usersList.get(i).getAge());
                        break;
                    case 5:
                        Users[i][j] = usersList.get(i).getAddress();
                        break;
                    case 6:
                        Users[i][j] = usersList.get(i).getuName();
                        break;
                    case 7:
                        Users[i][j] = usersList.get(i).getTelephone();
                        break;
                }
            }
        }

        table_book = new JTable(Users, title);
        table_book.setSize(500, 500);

        l_id.setBounds(10, 10, 80, 30);
        t_id.setBounds(100, 10, 80, 30);
        l_tip.setBounds(10, 50, 200, 30);
        b_query.setBounds(190, 10, 80, 30);
        this.add(l_id);
        this.add(t_id);
        this.add(l_tip);
        this.add(b_query);

        JScrollPane pane = new JScrollPane(table_book);
        pane.setBounds(10, 80, 500, 500);
        this.add(pane);

    }
}

class queryUserByUid extends JFrame{
    JTable table_book;
    AdminService adminService;
    String uId;

    public queryUserByUid(String uId) {
        this.uId = uId;
        this.setTitle("以下是"+uId+"用户所有信息:");
        this.setSize(450, 300);
        init();


    }

    public void init() {
        adminService = new AdminServiceImpl();
        User user = adminService.queryUser(uId);
        if (user==null){
            JOptionPane.showMessageDialog(this,
                    "请输入正确的用户ID", "！！！",JOptionPane.WARNING_MESSAGE);
            this.setVisible(false);
        }else {
            this.setVisible(true);
        }
        this.setLayout(null);
        try {
            Object[] title = {"用户ID", "密码", "身份证", "性别", "年龄", "地址", "姓名", "电话"};
            Object[][] Users = new String[1][8];
            for (int i = 0; i <1; i++) {
                for (int j = 0; j < 8; j++) {
                    switch (j) {
                        case 0:
                            Users[i][j] = user.getuId();
                            break;
                        case 1:
                            Users[i][j] = user.getPassword();
                            break;
                        case 2:
                            Users[i][j] = user.getIdCard();
                            break;
                        case 3:
                            Users[i][j] = user.getSex();
                            break;
                        case 4:
                            Users[i][j] = String.valueOf(user.getAge());
                            break;
                        case 5:
                            Users[i][j] = user.getAddress();
                            break;
                        case 6:
                            Users[i][j] = user.getuName();
                            break;
                        case 7:
                            Users[i][j] = user.getTelephone();
                            break;
                    }
                }

            }
            table_book = new JTable(Users, title);
            table_book.setSize(400, 300);
            JScrollPane pane = new JScrollPane(table_book);
            pane.setBounds(10, 40, 400, 40);
            this.add(pane);
        }catch (NullPointerException nullPointerException){
          //  System.out.println("用户没有请输入正确的用户ID 查询空 异常");
        }



    }

}
